package org.flashalchemy.ezlib.ezlog.console.skins
{
    import flash.display.GradientType;
    import mx.skins.Border;
    import mx.styles.StyleManager;
    import mx.utils.ColorUtil;
    import mx.skins.halo.HaloColors;


    /**
     * <b>Description:</b> The skin class for the scroll thumb of a <code>scrollBar</code>
     */
    public class MonochromaticScrollThumbSkin extends Border
    {
        //--------------------------------------------------------------------------
        //
        //  Class variables
        //
        //--------------------------------------------------------------------------
        /**
         *  @private
         */
        private static var cache:Object = {};

        //--------------------------------------------------------------------------
        //
        //  Class methods
        //
        //--------------------------------------------------------------------------
        /**
         *  @private
         *  Several colors used for drawing are calculated from the base colors
         *  of the component (themeColor, borderColor and fillColors).
         *  Since these calculations can be a bit expensive,
         *  we calculate once per color set and cache the results.
         */
        private static function calcDerivedStyles(themeColor:uint, borderColor:uint, fillColor0:uint, fillColor1:uint):Object
        {
            var key:String = HaloColors.getCacheKey(themeColor, borderColor, fillColor0, fillColor1);
            if (!cache[key])
            {
                var o:Object = cache[key] = {}
                // Cross-component styles.
                HaloColors.addHaloColors(o, themeColor, fillColor0, fillColor1);
                // ScrollArrowDown-specific styles.
                o.borderColorDrk1 = ColorUtil.adjustBrightness2(borderColor, -50);
            }
            return cache[key];
        }

        //--------------------------------------------------------------------------
        //
        //  Constructor
        //
        //--------------------------------------------------------------------------
        /**
         *  Constructor.
         */
        public function MonochromaticScrollThumbSkin()
        {
            super();
        }

        //--------------------------------------------------------------------------
        //
        //  Overridden properties
        //
        //--------------------------------------------------------------------------
        //----------------------------------
        //  measuredWidth
        //----------------------------------
        /**
         *  @private
         */
        override public function get measuredWidth():Number
        {
            return 16;
        }

        //----------------------------------
        //  measuredHeight
        //----------------------------------
        /**
         *  @private
         */
        override public function get measuredHeight():Number
        {
            return 10;
        }

        //--------------------------------------------------------------------------
        //
        //  Overridden methods
        //
        //--------------------------------------------------------------------------
        /**
         *  @private
         */
        override protected function updateDisplayList(w:Number, h:Number):void
        {
            super.updateDisplayList(w, h);
            // User-defined styles.
            var backgroundColor:Number = 0x458D14;
            var borderColor:uint = getStyle("borderColor");
            var cornerRadius:Number = getStyle("cornerRadius");
            var fillAlphas:Array = getStyle("fillAlphas");
            var fillColors:Array = getStyle("fillColors");
            StyleManager.getColorNames(fillColors);
            var highlightAlphas:Array = getStyle("highlightAlphas");
            var themeColor:uint = getStyle("themeColor");
            // Placeholder styles stub.
            var gripColor:uint = 0xFFFFFF;
            // Derived styles.
            var derStyles:Object = calcDerivedStyles(themeColor, borderColor, fillColors[0], fillColors[1]);
            var radius:Number = Math.max(cornerRadius - 1, 0);
            var cr:Object = { tl: radius, tr: radius, bl: radius, br: radius };
            radius = Math.max(radius - 1, 0);
            var cr1:Object = { tl: radius, tr: radius, bl: radius, br: radius };
            var horizontal:Boolean = parent && parent.parent && parent.parent.rotation != 0;
            if (isNaN(backgroundColor))
                backgroundColor = 0xFFFFFF;
            graphics.clear();
            // Opaque backing to force the scroll elements
            // to match other components by default.
            drawRoundRect(1, 0, w - 3, h, cr, backgroundColor, 1);
            switch (name)
            {
                default:
                case "thumbUpSkin":
                {
                    var upFillColors:Array = [ fillColors[0], fillColors[1]];
                    var upFillAlphas:Array = [ fillAlphas[0], fillAlphas[1]];
                    // positioning placeholder
                    drawRoundRect(0, 0, w, h, 0, 0xFFFFFF, 0);
                    // shadow removed
                    // border
                    drawRoundRect(1, 0, w - 3, h, cr, [ borderColor, derStyles.borderColorDrk1 ], 1, horizontal ? horizontalGradientMatrix(0, 0, w, h) : verticalGradientMatrix(0, 0, w, h), GradientType.
                                  LINEAR, null, { x: 1, y: 1, w: w - 4, h: h - 2, r: cr1 });
                    // fill
                    drawRoundRect(1, 1, w - 4, h - 2, cr1, upFillColors, upFillAlphas, horizontal ? horizontalGradientMatrix(1, 0, w - 2, h - 2) : verticalGradientMatrix(1, 0, w - 2, h - 2));
                    // highlight
                    if (horizontal)
                    {
                        drawRoundRect(1, 0, (w - 4) / 2, h - 2, 0, [ 0xFFFFFF, 0xFFFFFF ], highlightAlphas, horizontalGradientMatrix(1, 1, w - 4, (h - 2) / 2));
                    }
                    else
                    {
                        drawRoundRect(1, 1, w - 4, (h - 2) / 2, cr1, [ 0xFFFFFF, 0xFFFFFF ], highlightAlphas, horizontal ? horizontalGradientMatrix(1, 0, (w - 4) / 2, h - 2) : verticalGradientMatrix(1,
                                                                                                                                                                                                       1,
                                                                                                                                                                                                       w -
                                                                                                                                                                                                       4,
                                                                                                                                                                                                       (h -
                                                                                                                                                                                                       2) /
                                                                                                                                                                                                       2));
                    }
                    break;
                }
                case "thumbOverSkin":
                {
                    var upFillColorss:Array = [ fillColors[0], fillColors[1]];
                    var upFillAlphass:Array = [ fillAlphas[0], fillAlphas[1]];
                    // positioning placeholder
                    drawRoundRect(0, 0, w, h, 0, 0xFFFFFF, 0);
                    // shadow removed
                    // border
                    drawRoundRect(1, 0, w - 2.5, h, cr, [ 0x1E3C09, 0x1E3C09 ], 1);
                    // fill
                    drawRoundRect(1, 1, w - 4, h - 2, cr1, upFillColorss, upFillAlphass, horizontal ? horizontalGradientMatrix(1, 0, w - 2, h - 2) : verticalGradientMatrix(1, 0, w - 2, h - 2));
                    // highlight
                    if (horizontal)
                    {
                        drawRoundRect(1, 0, (w - 4) / 2, h - 2, 0, [ 0xFFFFFF, 0xFFFFFF ], 1.0, horizontalGradientMatrix(1, 1, w - 4, (h - 2) / 2));
                    }
                    else
                    {
                        drawRoundRect(1, 1, w - 4, (h - 2) / 2, cr1, [ 0xFFFFFF, 0xFFFFFF ], highlightAlphas, horizontal ? horizontalGradientMatrix(1, 0, (w - 4) / 2, h - 2) : verticalGradientMatrix(1,
                                                                                                                                                                                                       1,
                                                                                                                                                                                                       w -
                                                                                                                                                                                                       4,
                                                                                                                                                                                                       (h -
                                                                                                                                                                                                       2) /
                                                                                                                                                                                                       2));
                    }
                    break;
                }
                case "thumbDownSkin":
                {
                    // shadow removed
                    // border
                    drawRoundRect(1, 0, w - 3, h, cr, [ themeColor, derStyles.themeColDrk2 ], 1, horizontal ? horizontalGradientMatrix(1, 0, w, h) : verticalGradientMatrix(0, 0, w, h), GradientType.LINEAR,
                                  null, { x: 1, y: 1, w: w - 4, h: h - 2, r: cr1 });
                    // fill
                    drawRoundRect(1, 1, w - 4, h - 2, cr1, [ 0x9DEA6A, 0x000000 ], 1, horizontal ? horizontalGradientMatrix(1, 0, w, h) : verticalGradientMatrix(1, 0, w, h));
                    break;
                }
                case "thumbDisabledSkin":
                {
                    // positioning placeholder
                    drawRoundRect(0, 0, w, h, 0, 0xFFFFFF, 0);
                    // border
                    drawRoundRect(1, 0, w - 3, h, cr, 0x999999, 0.5);
                    // fill
                    drawRoundRect(1, 1, w - 4, h - 2, cr1, 0xFFFFFF, 0.5);
                    break;
                }
            }
            // Draw grip.
            var gripW:Number = Math.floor(w / 2 - 4);
            drawRoundRect(gripW, Math.floor(h / 2 - 4), 5, 1, 0, 0x1E3C09, 1.0);
            drawRoundRect(gripW, Math.floor(h / 2 - 2), 5, 1, 0, 0x1E3C09, 1.0);
            drawRoundRect(gripW, Math.floor(h / 2), 5, 1, 0, 0x1E3C09, 1.0);
            drawRoundRect(gripW, Math.floor(h / 2 + 2), 5, 1, 0, 0x1E3C09, 1.0);
            drawRoundRect(gripW, Math.floor(h / 2 + 4), 5, 1, 0, 0x1E3C09, 1.0);
        }
    }
}

